<template>
    <div>
        <van-nav-bar title="宠物寄养" left-text="返回" left-arrow @click-left="goBack"></van-nav-bar>
        
        <van-contact-edit 
            :is-edit="false" 
            show-set-default 
            :contact-info="editingContact" 
            set-default-label="设为默认联系人"
             @save="onSave"
             :tel-validator="validatorTel" />
    </div>
</template>

<script>
export default {
    data() {
        return {
            editingContact: {},
        };
    },
    methods: {
        validatorTel(tel){
            return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(tel)
        },
        goBack(){
            this.$router.back()
        },
        // 保存联系人
        onSave(contactInfo) {
            console.log(contactInfo);
            // 获取联系人数据
            let {name,tel,isDefault} = contactInfo
            if(isDefault) {
                isDefault = 1
            } else {
                isDefault = 0
            }
            this.http.post(`${this.$api.addContact}`,{name,tel,isDefault})
                .then(res=> res.data)
                .then(res => {
                    if(res.code == 200) {
                        this.$msg.success(res.msg)
                        // 新增成功，返回
                        setTimeout(()=>{
                            this.$router.back()
                        },1000)
                    } else {
                        this.$msg.fail(res.msg)
                    }
                })
        },

    },
}
</script>

<style></style>